1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package com.germinus.easyconf;
17
18 import org.apache.commons.digester.Digester;
19 import org.apache.commons.digester.Substitutor;
20 import org.apache.commons.digester.substitution.MultiVariableExpander;
21 import org.apache.commons.digester.substitution.VariableSubstitutor;
22 import org.apache.commons.digester.xmlrules.DigesterLoader;
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.xml.sax.SAXException;
26
27 import java.io.IOException;
28 import java.io.FileNotFoundException;
29 import java.net.URL;
30
31 /***
32 * Handles the actual reading of the configuration
33 *
34 * @author jferrer
35 */
36 class ConfigurationLoader {
37 private static final Log log = LogFactory.getLog(ConfigurationLoader.class);
38
39
40 public ComponentProperties readPropertiesConfiguration(String companyId, String componentName) {
41 AggregatedProperties properties = new AggregatedProperties(companyId, componentName);
42
43
44
45
46 properties.addGlobalFileName(Conventions.GLOBAL_CONFIGURATION_FILE +
47 Conventions.PROPERTIES_EXTENSION);
48
49
50
51
52 properties.addBaseFileName(componentName + Conventions.PROPERTIES_EXTENSION);
53
54 log.info("Properties for " + componentName + " loaded from " + properties.loadedSources());
55 return new ComponentProperties(properties);
56 }
57
58 public ConfigurationObjectCache readConfigurationObject(String companyId,
59 String componentName, ComponentProperties properties)
60 throws IOException, SAXException {
61 log.info("Reading the configuration object for " + componentName);
62
63 String confFileName = null;
64 URL confFile = null;
65 if (companyId != null) {
66 confFileName = componentName + Conventions.SLASH + companyId + Conventions.XML_EXTENSION;
67 confFile = ClasspathUtil.locateResource(null, confFileName);
68 log.info("Loaded " + confFileName + ": " + confFile);
69 }
70 if (confFile == null) {
71 confFileName = componentName + Conventions.XML_EXTENSION;
72 confFile = ClasspathUtil.locateResource(null, confFileName);
73 }
74 if (confFile == null) {
75 throw new FileNotFoundException("File " + confFileName + " not found");
76 }
77 Object confObj = loadXMLFile(confFile, properties);
78 ConfigurationObjectCache result = new ConfigurationObjectCache(confObj, confFile, properties);
79 Long delay = properties.getDelayPeriod();
80 if (delay != null) {
81 result.setReloadingStrategy(
82 new FileURLChangedReloadingStrategy(confFile, delay.longValue()));
83 }
84 return result;
85
86 }
87
88 /***
89 * Read an XML file and return an Object representation of its contents
90 */
91 Object loadXMLFile(URL confFileUrl, ComponentProperties properties)
92 throws IOException, SAXException {
93 log.debug("Loading XML file: " + confFileUrl);
94 String componentName = properties.getComponentName();
95 String rulesFileName = componentName + Conventions.DIGESTERRULES_EXTENSION;
96 URL digesterRulesUrl = ClasspathUtil.locateResource(rulesFileName);
97 if (digesterRulesUrl == null) {
98 throw new DigesterRulesNotFoundException(componentName,
99 rulesFileName);
100 }
101 Digester digester = DigesterLoader.createDigester(digesterRulesUrl);
102 digester.setUseContextClassLoader(true);
103 digester.setValidating(false);
104
105 MultiVariableExpander expander = new MultiVariableExpander();
106 expander.addSource("$", properties.toMap());
107 Substitutor substitutor = new VariableSubstitutor(expander);
108 digester.setSubstitutor(substitutor);
109
110 try {
111 Object confObj = digester.parse(confFileUrl.openStream());
112 log.info("Read configuration from " + confFileUrl);
113 return confObj;
114 } catch (IllegalArgumentException e) {
115
116 throw new InvalidPropertyException(properties.getComponentName(), e);
117 }
118 }
119
120 }